package br.ujr.pocs.activemq.queue;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * Hello world!
 *
 */
public class ProducerQueue 
{
    public static void main( String[] args )
    {
    	String url = "tcp://localhost:61616";
    	Connection connection = null;
    	Session session = null;
    	
    	try {
    		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
    		connection = factory.createConnection();
    		connection.start();
    		
    		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    		Destination destination = session.createQueue("TEST.FOO");
    		MessageProducer messageProducer = session.createProducer(destination);
    		messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    		
    		TextMessage message = session.createTextMessage("Hello");
    		messageProducer.send(message);
    		
    		System.out.println("Sent...");
    		
    	} catch (JMSException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				if ( session != null ) session.close();
				if ( connection != null ) connection.close();
			} catch (JMSException e) {
				throw new RuntimeException(e);
			}
    	}
    }
}
